<!--
  @Description:页面管理-新增/编辑
  @Author:zqy
  @Date:2023-09-01 15:28:26
  @Last Modified by:zqy
  @Last Modified time:2023-09-01 15:28:26
-->
<template>
  <el-dialog
    ref="refDlg"
    :title="detailData===''? '新增页面' : '编辑页面'"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :hide-func-bar="true"
    :before-close="clearForm"
    width="600px"
    custom-class="custom-dlg"
    @close="closeDialog"
  >
    <div>
      <el-form ref="form" :model="formData" :rules="formRules" size="mini" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="页面名称" prop="pageName">
              <el-input v-model="formData.pageName" size="mini" laceholder="请输入页面名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="页面地址" prop="pageUrl">
              <el-input v-model="formData.pageUrl" size="mini" laceholder="请输入页面地址" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="formData.remark"
                size="mini"
                maxlength="50"
                placeholder="请输入备注"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">保存</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { savePageManager } from '@/api/pageManagement' // 新增/编辑
export default {
  name: 'PageEditForm',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      submiting: false,
      formData: {
        pageName: '',
        pageUrl: '',
        remark: ''
      },
      formRules: {
        pageName: [{
          required: true,
          message: '请输入页面名称',
          trigger: 'blur'
        }],
        pageUrl: [{
          required: true,
          message: '请输入页面地址',
          trigger: 'blur'
        }]
      }
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    if (this.detailData && this.detailData.id) {
      this.formData = Object.assign({}, this.detailData)
    }
  },
  methods: {
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        pageName: '',
        pageUrl: '',
        remark: ''
      }
      this.$refs.form && this.$refs.form.clearValidate()
      done && done()
    },
    // 提交表单处理
    submitForm() {
      if (this.submiting) return
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          const formData = Object.assign(this.formData)
          if (this.detail && this.detail.id) {
            formData.id = this.detail.id
          }
          this.submiting = true
          savePageManager(formData).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message.success('保存成功')
              this.$emit('success', !formData.id)
              this.closeDialog()
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>

</style>
